package com.rabbiter.oes.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbiter.oes.entity.College;
import com.rabbiter.oes.result.R;
import com.rabbiter.oes.service.impl.CollegeServiceImpl;
import com.rabbiter.oes.vo.CollegeListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "学院")
@RestController
public class CollegeController {

    @Autowired
    private CollegeServiceImpl collegeService;

    @ApiOperation(value = "分页查询学院信息", notes = "分页查询学院信息", httpMethod = "GET")
    @GetMapping("/colleges/{page}/{size}/{name}")
    public R<IPage<CollegeListVO>> findPage(@PathVariable Integer page, @PathVariable("size") Integer size, @PathVariable("name") String name) {
        Page<CollegeListVO> collegePage = new Page<>(page, size);
        IPage<CollegeListVO> collegeIPage = collegeService.findPage(collegePage, name);
        return R.<IPage<CollegeListVO>>ok("分页查询学院信息").data(collegeIPage);
    }

    @ApiOperation(value = "查询全部学院信息", notes = "查询全部学院信息", httpMethod = "GET")
    @GetMapping("/findAllCollege")
    public R<List<College>> findAllCollege() {
        List<College> colleges = collegeService.findAllCollege();
        return R.<List<College>>ok("学院信息").data(colleges);
    }


    @ApiOperation(value = "根据id查询学院信息", notes = "根据id查询学院信息", httpMethod = "GET")
    @GetMapping("/college/{id}")
    public R<College> findById(@PathVariable("id") Integer id) {
        College college = collegeService.findById(id);
        return R.<College>ok().data(college);
    }

    @ApiOperation(value = "根据id删除学院信息", notes = "根据id删除学院信息", httpMethod = "DELETE")
    @DeleteMapping("/college/{id}")
    public R deleteById(@PathVariable("id") Integer id) {
        collegeService.deleteById(id);
        return R.ok("删除成功");
    }

    @ApiOperation(value = "修改学院信息", notes = "修改学院信息", httpMethod = "PUT")
    @PutMapping("/college")
    public R update(@RequestBody College college) {
        String name = college.getName();
        Long countByName = collegeService.findCountByName(name);
        if (countByName == 0) {
            collegeService.update(college);
            return R.ok().data(true);
        } else {
            return R.ok("学院已存在！").data(false);
        }
    }
    
    @ApiOperation(value = "添加学院信息", notes = "添加学院信息", httpMethod = "POST")
    @PostMapping("/college")
    public R add(@RequestBody College college) {
        String name = college.getName();
        Long countByName = collegeService.findCountByName(name);
        if (countByName == 0) {
            collegeService.add(college);
            return R.ok().data(true);
        } else {
            return R.ok("学院已存在！").data(false);
        }
    }
}
